package jnpf.util;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import jnpf.base.ActionResult;
import jnpf.base.UserInfo;
import jnpf.base.entity.DictionaryDataEntity;
import jnpf.base.entity.ProvinceEntity;
import jnpf.base.entity.VisualdevEntity;
import jnpf.base.model.VisualWebTypeEnum;
import jnpf.base.service.*;
import jnpf.engine.entity.FlowEngineEntity;
import jnpf.engine.entity.FlowTaskEntity;
import jnpf.engine.model.flowengine.FlowModel;
import jnpf.engine.service.FlowEngineService;
import jnpf.engine.service.FlowTaskNewService;
import jnpf.engine.service.FlowTaskService;
import jnpf.exception.DataException;
import jnpf.exception.WorkFlowException;
import jnpf.model.visiual.JnpfKeyConsts;
import jnpf.model.visiual.OnlineDevData;
import jnpf.model.visiual.superQuery.ConditionJsonModel;
import jnpf.model.visiual.superQuery.SuperQueryConditionModel;
import jnpf.onlinedev.model.VisualdevModelDataInfoVO;
import jnpf.onlinedev.service.VisualDevInfoService;
import jnpf.onlinedev.service.VisualdevModelDataService;
import jnpf.onlinedev.util.onlineDevUtil.OnlinePublicUtils;
import jnpf.permission.entity.*;
import jnpf.permission.service.*;
import jnpf.permission.util.PermissionUtil;
import jnpf.util.context.SpringContext;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.lang.reflect.Field;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 数据转换(代码生成器用)
 *
 * @author JNPF开发平台组
 * @version V3.1.0
 * @copyright 引迈信息技术有限公司（https://www.jnpfsoft.com）
 * @date 2021/3/16
 */
@Component
public class GeneraterSwapUtil {

	@Autowired
	private OrganizeService organizeService;

	@Autowired
	private PositionService positionService ;

	@Autowired
	private UserService userService ;

	@Autowired
	private VisualdevService visualdevService ;

	@Autowired
	private VisualDevInfoService visualDevInfoService;

	@Autowired
	private DataInterfaceService dataInterfaceService ;

	@Autowired
	private VisualdevModelDataService visualdevModelDataService ;

	@Autowired
	private ProvinceService provinceService;

	@Autowired
	private DictionaryDataService dictionaryDataService;

	@Autowired
	private BillRuleService billRuleService;

	@Autowired
	private FlowTaskService flowTaskService;

	@Autowired
	private GroupService groupService;

	@Autowired
	private RoleService roleService;

	public final String regEx = "[\\[\\]\"]";
	/**
	 * 日期时间戳字符串转换
	 *
	 * @param date
	 * @param format
	 * @return
	 */
	public String dateSwap(String date, String format) {
		if (StringUtil.isNotEmpty(date)) {
			DateTimeFormatter ftf = DateTimeFormatter.ofPattern(format);
			if (date.contains(",")) {
				String[] dates = date.split(",");
				long time1 = Long.parseLong(dates[0]);
				long time2 = Long.parseLong(dates[1]);
				String value1 = ftf.format(LocalDateTime.ofInstant(Instant.ofEpochMilli(time1), ZoneId.systemDefault()));
				String value2 = ftf.format(LocalDateTime.ofInstant(Instant.ofEpochMilli(time2), ZoneId.systemDefault()));
				return value1 + "至" + value2;
			}
			long time = Long.parseLong(date);
			String value = ftf.format(LocalDateTime.ofInstant(Instant.ofEpochMilli(time), ZoneId.systemDefault()));
			return value;
		}
		return date;
	}

	/**
	 * 行政区划转换
	 *
	 * @param data
	 * @return
	 */
	public String provinceData(String data) {
		if (StringUtil.isNotEmpty(data)) {
			try {
				if (data.contains("[[")){
					List<String> addList = new ArrayList<>();
					String[][] provinceDataS = JsonUtil.getJsonToBean(data, String[][].class);
					for (String[] AddressData :provinceDataS){
						List<String> provList = new ArrayList(Arrays.asList(AddressData));
						List<ProvinceEntity> proList = provinceService.getProList(provList);
						//单条
						List<String> adList = proList.stream().map(pro->pro.getFullName()).collect(Collectors.toList());
						addList.add(String.join("/", adList));
					}
					return String.join(";",addList);
				}else if (data.contains("[")) {
					List<String> idList = JsonUtil.getJsonToList(data, String.class);
					List<String> nameList = new ArrayList<>();
					for (String orgId : idList) {
						ProvinceEntity info = provinceService.getInfo(orgId);
						nameList.add(Objects.nonNull(info) ? info.getFullName() : orgId);
					}
					return String.join("/",nameList);
				} else {
					String[] strs = data.split(",");
					List<String> provList = new ArrayList(Arrays.asList(strs));
					List<String> proNameList = new ArrayList<>();
					List<ProvinceEntity> proList = provinceService.getProList(provList);
					for (ProvinceEntity entity : proList){
						proNameList.add(entity.getFullName());
					}
					return String.join("/",proNameList);
				}
			}catch (Exception e){
				e.printStackTrace();
			}
		}
		return "";
	}

	/**
	 * 所属组织id转名称
	 *
	 * @param value
	 * @return
	 */
	public String comSelectValue(String value,String showLevel) {
		if (StringUtil.isNotEmpty(String.valueOf(value))) {
			OrganizeEntity organizeEntity = organizeService.getInfo(String.valueOf(value));
			if (!"all".equals(showLevel)) {
				if (organizeEntity.getCategory().equals("company")){
					return " ";
				}
				value= PermissionUtil.getLinkInfoByOrgId(value, organizeService, false);
			} else {
				if (organizeEntity != null) {
					value = organizeEntity.getFullName();
				}
			}
		} else {
			value = " ";
		}
		return value;
	}

	/**
	 * 公司部门id转名称(多选)
	 *
	 * @param ids
	 * @return
	 */
	public String comSelectValues(String ids,Boolean mul) {
		List<String> comValueList= new ArrayList<>();
		if (StringUtil.isEmpty(ids)){
			return null;
		}
		String Separator = mul ? "," : "/";
		if (ids.contains("[[")){
			String[][] idArrays = JsonUtil.getJsonToBean(ids, String[][].class);
			for (String[] array : idArrays){
				List<String> idList = new ArrayList<>();
				for (String s : array){
					OrganizeEntity info = organizeService.getInfo(s);
					idList.add(Objects.nonNull(info) ? info.getFullName() : s );
				}
				String orgCom = idList.stream().collect(Collectors.joining("/"));
				comValueList.add(orgCom);
			}
			return comValueList.stream().collect(Collectors.joining(";"));
		}else if (ids.contains("[")){
			List<String> idList=JsonUtil.getJsonToList(ids,String.class);
			List<String> nameList= new ArrayList<>();
			for (String orgId : idList){
				OrganizeEntity info = organizeService.getInfo(orgId);
				nameList.add(Objects.nonNull(info) ? info.getFullName() : orgId);
			}
			return nameList.stream().collect(Collectors.joining(Separator));
		}else {
			ids = ids.replaceAll("\"","");
			String[] idList = ids.split(",");
			if (idList.length > 0) {
				List<String> comSelectList = new ArrayList<>();
				for (String id : idList) {
					OrganizeEntity organizeEntity = organizeService.getInfo(id);
					if (organizeEntity != null) {
						comSelectList.add(organizeEntity.getFullName());
					}
				}
				return String.join(",", comSelectList);
			}
		}
		return null;
	}


	/**
	 * 岗位id转名称
	 *
	 * @param id
	 * @return
	 */
	public String posSelectValue(String id) {
		if (StringUtil.isNotEmpty(id)) {
			PositionEntity positionServiceInfo = positionService.getInfo(id);
			if (ObjectUtil.isNotEmpty(positionServiceInfo)) {
				return positionServiceInfo.getFullName();
			}
			return id;
		}
		return " ";
	}

	/**
	 * 岗位id转名称(多选)
	 *
	 * @param ids
	 * @return
	 */
	public String posSelectValues(String ids) {
		if (StringUtil.isEmpty(ids)){
			return "";
		}
		List<String> posList = new ArrayList<>();
		if (ids.contains("[")){
			List<String> idList=JsonUtil.getJsonToList(ids,String.class);
			List<String> nameList= new ArrayList<>();
			for (String orgId : idList){
				PositionEntity info = positionService.getInfo(orgId);
				nameList.add(Objects.nonNull(info) ? info.getFullName() : orgId);
			}
			posList = nameList;
		}else {
			String[] idList = ids.split(",");
			if (idList.length > 0) {
				for (String id : idList) {
					PositionEntity positionEntity = positionService.getInfo(id);
					if (ObjectUtil.isNotEmpty(positionEntity)) {
						posList.add(positionEntity.getFullName());
					}
				}
			}
		}
		return String.join(",", posList);
	}

	/**
	 * 用户id转名称
	 *
	 * @param id
	 * @return
	 */
	public String userSelectValue(String id) {
		if (StringUtil.isNotEmpty(id)) {
			UserEntity userEntity = userService.getInfo(id);
			if (ObjectUtil.isNotEmpty(userEntity)) {
				return userEntity.getRealName() + "/" + userEntity.getAccount();
			}
			return id;
		}
		return "";
	}

	/**
	 * 用户id转名称(多选)
	 *
	 * @param ids
	 * @return
	 */
	public String userSelectValues(String ids) {
		if (StringUtil.isEmpty(ids)) {
			return ids;
		}
		if (ids.contains("[")){
			List<String> nameList = new ArrayList<>();
			List<String> jsonToList = JsonUtil.getJsonToList(ids, String.class);
			for (String userId : jsonToList){
				UserEntity info = userService.getInfo(userId);
				nameList.add(Objects.nonNull(info) ? info.getRealName()+ "/" + info.getAccount() : userId);
			}
			return String.join(";", nameList);
		}else {
			List<String> userInfoList = new ArrayList<>();
			String[] idList = ids.split(",");
			if (idList.length > 0) {
				for (String id : idList) {
					UserEntity userEntity = userService.getInfo(id);
					if (ObjectUtil.isNotEmpty(userEntity)) {
						String info = userEntity.getRealName() + "/" + userEntity.getAccount();
						userInfoList.add(info);
					}
				}
			}
			return String.join("-", userInfoList);
		}
	}

	/**
	 * 开关
	 *
	 * @param data
	 * @return
	 */
	public String switchSelectValue(String data,String activeTxt,String inactiveTxt) {
		if (StringUtil.isNotEmpty(data)) {
			if (data.equals("0") || data.equals("false")) {
				return inactiveTxt;
			} else if (data.equals("1") || data.equals("true")) {
				return activeTxt;
			} else {
				return data;
			}
		}
		return null;
	}

	/**
	 * 关联表单数据转换
	 *
	 * @param vmodel
	 * @param value
	 * @param modelId
	 * @return
	 */
	public String swapRelationFormValue(String vmodel, String value, String modelId, Map<String, Object> formDataMaps) {
		if (StringUtil.isEmpty(value)) {
			return "";
		}
		try {
			VisualdevModelDataInfoVO infoVO = null;
			VisualdevEntity entity = visualdevService.getInfo(modelId);
			if (!StringUtil.isEmpty(entity.getVisualTables()) && !OnlineDevData.TABLE_CONST.equals(entity.getVisualTables())) {
				infoVO = visualDevInfoService.getDetailsDataInfo(value, entity) ;
			}else {
				infoVO = visualdevModelDataService.infoDataChange(value, entity);
			}
			if(infoVO!=null) {
				Map<String, Object> formDataMap = infoVO.getData()!=null ? JsonUtil.stringToMap(infoVO.getData()) : new HashMap<>() ;
				if (formDataMap.size() > 0) {
					formDataMaps.putAll(formDataMap);
					formDataMap = OnlinePublicUtils.mapKeyToLower(formDataMap);
					value = String.valueOf(formDataMap.get(vmodel.toLowerCase()));
				}
			}
		}catch (Exception e){
			e.printStackTrace();
		}
		return value;
	}

	/**
	 * 弹窗
	 *
	 * @param interfaceId
	 * @param propsValue
	 * @param relationField
	 * @param dataValue
	 * @return
	 */
	public String getPopupSelectValue(String interfaceId, String propsValue, String relationField, String dataValue,Map<String, Object> dataMaps) {
		if (StringUtil.isEmpty(interfaceId)){
			return null;
		}
		if (StringUtil.isNotEmpty(dataValue)) {
			Object data = dataInterfaceService.infoToId(interfaceId, null, null).getData();
			List<Map<String, Object>> dataInterfaceDataList;
			if (data instanceof ActionResult){
				ActionResult actionVo = (ActionResult) data;
				dataInterfaceDataList = (List<Map<String, Object>>) actionVo.getData();
			}else {
				dataInterfaceDataList = (List<Map<String, Object>>) data;
			}
			if (dataValue.contains("[")){
				List<String> valueList = JsonUtil.getJsonToList(dataValue, String.class);
				List<String> swapValue = new ArrayList<>();
				for (String va : valueList){
					dataInterfaceDataList.stream().filter(map ->
							map.get(propsValue).equals(va)
					).forEach(
							modelMap -> swapValue.add(String.valueOf(modelMap.get(relationField)))
					);
				}
					return swapValue.stream().collect(Collectors.joining(","));
			}
			Map<String, Object>	dataMap = dataInterfaceDataList.stream().filter(d -> d.get(propsValue).equals(dataValue)).findFirst().orElse(null);
			if (dataMap!=null){
				dataMaps.putAll(dataMap);
				return String.valueOf(dataMap.get(relationField));
			}
			return null;
		} else {
			return null;
		}
	}


	public String getFileNameInJson(String fileJson){
		if (StringUtil.isNotEmpty(fileJson) && !"null".equals(fileJson) ){
			return fileJson;
		}
		return "";
	}

	/**
	 * 获取数据字典数据
	 *
	 * @param feild
	 * @return
	 */
	public String getDicName(String feild,String dictionaryTypeId) {
		if (StringUtil.isNotEmpty(feild)) {
			//去除中括号以及双引号
			feild = feild.replaceAll(regEx, "");
			//判断多选框
			String[] feilds = feild.split(",");
			if (feilds.length > 1) {
				StringBuilder feildsValue = new StringBuilder();
				DictionaryDataEntity dictionaryDataEntity;
				for (String feil : feilds) {
					dictionaryDataEntity = dictionaryDataService.getSwapInfo(feil,dictionaryTypeId);
					if (dictionaryDataEntity != null) {
						feildsValue.append(dictionaryDataEntity.getFullName() + ",");
					}
				}
				String finalValue;
				if (StringUtil.isEmpty(feildsValue)) {
					finalValue = feildsValue.toString();
				} else {
					finalValue = feildsValue.substring(0, feildsValue.length() - 1);
				}
				return finalValue;
			}
			DictionaryDataEntity dictionaryDataentity = dictionaryDataService.getSwapInfo(feild,dictionaryTypeId);
			if (dictionaryDataentity != null) {
				return dictionaryDataentity.getFullName();
			}
			return feild;
		}
		return feild;
	}

	/**
	 * 获取远端数据
	 *
	 * @param urlId
	 * @param label
	 * @param value
	 * @param feildValue
	 * @return
	 * @throws IOException
	 */
	public String getDynName(String urlId, String label, String value, String feildValue) {
		if (StringUtil.isNotEmpty(feildValue)) {
			//去除中括号以及双引号
			feildValue = feildValue.replaceAll(regEx, "");
			//获取远端数据
			ActionResult object = dataInterfaceService.infoToId(urlId, null, null);
			if (object.getData() != null && object.getData() instanceof List) {
				List<Map<String, Object>> dataList = (List<Map<String, Object>>) object.getData();
//				List<Map<String, Object>> dataList = (List<Map<String, Object>>) vo.getData();
				//判断是否多选
				String[] feildValues = feildValue.split(",");
				if (feildValues.length > 0) {
					//转换的真实值
					StringBuilder feildVa = new StringBuilder();
					for (String feild : feildValues) {
						for (Map<String, Object> data : dataList) {
							if (String.valueOf(data.get(value)).equals(feild)) {
								feildVa.append(data.get(label) + ",");
							}
						}
					}
					String finalValue;
					if (StringUtil.isEmpty(feildVa)) {
						finalValue = feildVa.toString();
					} else {
						finalValue = feildVa.substring(0, feildVa.length() - 1);
					}
					return finalValue;
				}
				for (Map<String, Object> data : dataList) {
					if (feildValue.equals(String.valueOf(data.get(value)))) {
						return data.get(label).toString();
					}
					return feildValue;
				}
			}
			return feildValue;
		}
		return feildValue;
	}

	/**
	 * 获取远端数据
	 *
	 * @param urlId
	 * @param name
	 * @param id
	 * @param children
	 * @param feildValue
	 * @return
	 */
	public String getDynName(String urlId, String name, String id, String children, String feildValue, boolean mul) {
		List<String> result = new ArrayList<>();
		String sep = ",";
		if (mul){
			sep = "/";
		}
		if (StringUtil.isNotEmpty(feildValue)) {
			ActionResult object = dataInterfaceService.infoToId(urlId, null, null);
//			ActionResult actionVo = (ActionResult) object.getData();
			List<Map<String, Object>> dataList = (List<Map<String, Object>>) object.getData();
//			if (actionVo.getData() instanceof List) {
//				dataList = (List<Map<String, Object>>) actionVo.getData();
//			}
			JSONArray dataAll = JsonUtil.getListToJsonArray(dataList);
			List<Map<String, String>> list = new ArrayList<>();
			treeToList(id, name, children, dataAll, list);
			String value = feildValue.replaceAll("\\[", "").replaceAll("\\]", "");
			Map<String,String> resultMap = new HashMap<>();
			list.stream().forEach(t -> {
				resultMap.put(String.valueOf(t.get(id)),String.valueOf(t.get(name)));
			});

			if (feildValue.startsWith("[[")){
				String[][] fv = JsonUtil.getJsonToBean(feildValue, String[][].class);
				StringJoiner f1= new StringJoiner(",");
				for (String[] f: fv){
					StringJoiner v1 = new StringJoiner("/");
					for (String v : f){
						v1.add(resultMap.get(v));
					}
					f1.add(v1.toString());
				}
				return f1.toString();
			}else if (feildValue.startsWith("[")){
				List<String> fvs = JsonUtil.getJsonToList(feildValue, String.class);
			  return fvs.stream().map(m->resultMap.get(m)).collect(Collectors.joining(sep));
			}else {
				return resultMap.get(feildValue);
			}
		}
		return feildValue;
	}


	/**
	 * 获取远端数据
	 *
	 * @param urlId
	 * @param name
	 * @param id
	 * @param children
	 * @param feildValue
	 * @param mul 是否多选
	 * @param isFullPath 全路径
	 * @return
	 */
	public String getDynName(String urlId, String name, String id, String children, String feildValue, boolean mul, boolean isFullPath) {
		List<String> result = new ArrayList<>();
		String sep = ",";
		if (mul){
			sep = "/";
		}
		if (StringUtil.isNotEmpty(feildValue)) {
			ActionResult object = dataInterfaceService.infoToId(urlId, null, null);
//			ActionResult actionVo = (ActionResult) object.getData();
			List<Map<String, Object>> dataList = (List<Map<String, Object>>) object.getData();
//			if (actionVo.getData() instanceof List) {
//				dataList = (List<Map<String, Object>>) actionVo.getData();
//			}
			JSONArray dataAll = JsonUtil.getListToJsonArray(dataList);
			List<Map<String, String>> list = new ArrayList<>();
			treeToList(id, name, children, dataAll, list);
			String value = feildValue.replaceAll("\\[", "").replaceAll("\\]", "");
			Map<String,String> resultMap = new HashMap<>();
			list.stream().forEach(t -> {
				resultMap.put(String.valueOf(t.get(id)),String.valueOf(t.get(name)));
			});

			if (feildValue.startsWith("[[")){
				String[][] fv = JsonUtil.getJsonToBean(feildValue, String[][].class);
				StringJoiner f1= new StringJoiner(",");
				for (String[] f: fv){
					StringJoiner v1 = new StringJoiner("/");
					if (isFullPath){
						for (String v : f){
							v1.add(resultMap.get(v));
						}
					} else {
						v1.add(resultMap.get(f[f.length-1]));
					}
					f1.add(v1.toString());
				}
				return f1.toString();
			}else if (feildValue.startsWith("[")){
				List<String> fvs = JsonUtil.getJsonToList(feildValue, String.class);
				if (isFullPath){
					return fvs.stream().map(m->resultMap.get(m)).collect(Collectors.joining(sep));
				} else {
					return resultMap.get(fvs.get(fvs.size()-1));
				}
			}else {
				return resultMap.get(feildValue);
			}
		}
		return feildValue;
	}


	/**
	 * 树转成list
	 **/
	private void treeToList(String id, String fullName, String children, JSONArray data, List<Map<String, String>> result) {
		for (int i = 0; i < data.size(); i++) {
			JSONObject ob = data.getJSONObject(i);
			Map<String, String> tree = new HashMap<>(16);
			tree.put(id, String.valueOf(ob.get(id)));
			tree.put(fullName, String.valueOf(ob.get(fullName)));
			result.add(tree);
			if (ob.get(children) != null) {
				JSONArray childArray = ob.getJSONArray(children);
				treeToList(id, fullName, children, childArray, result);
			}
		}
	}

	/**
	 * 生成单据规则
	 * @param encode
	 * @param isCache
	 * @return
	 * @throws DataException
	 */
	public String getBillNumber(String encode, Boolean isCache) throws DataException {
		return billRuleService.getBillNumber(encode, isCache);
	}

	/**
	 * 功能流程 获取可视化实体
	 * @param visualId
	 * @return
	 */
	public VisualdevEntity getVisualEntity(String visualId){
		VisualdevEntity info = visualdevService.getInfo(visualId);
		if (info!=null){
			return info;
		}
		return new VisualdevEntity();
	}

	public UserEntity getUser(String userId){
		return userService.getInfo(userId);
	}


	/**
	 * 获取流程任务
	 * @param id
	 * @param columns
	 * @return
	 */
	public FlowTaskEntity getInfoSubmit(String id, SFunction<FlowTaskEntity, ?>... columns) {
		return flowTaskService.getInfoSubmit(id, columns);
	}

	public void deleteFlowTask(FlowTaskEntity flowTaskEntity) throws WorkFlowException {
		flowTaskService.delete(flowTaskEntity);
	}

	public String getGroupSelect(String groupIds){
		if (StringUtil.isEmpty(groupIds)){
			return groupIds;
		}
		List<String> swapList = new ArrayList<>();
		if (groupIds.contains("[")){
			List<String> groups = JsonUtil.getJsonToList(groupIds, String.class);
			for (String g : groups){
				GroupEntity info = groupService.getInfo(g);
				String s = info != null ? info.getFullName() : "";
				swapList.add(s);
			}
		} else {
			GroupEntity info = groupService.getInfo(groupIds);
			swapList.add(info != null ? info.getFullName() : "");
		}
		return swapList.stream().collect(Collectors.joining(","));
	}

	public String getRoleSelect(String roleIds){
		if (StringUtil.isEmpty(roleIds)){
			return roleIds;
		}
		List<String> swapList = new ArrayList<>();
		if (roleIds.contains("[")){
			List<String> groups = JsonUtil.getJsonToList(roleIds, String.class);
			for (String g : groups){
				RoleEntity info = roleService.getInfo(g);
				String s = info != null ? info.getFullName() : "";
				swapList.add(s);
			}
		} else {
			RoleEntity info = roleService.getInfo(roleIds);
			swapList.add(info != null ? info.getFullName() : "");
		}
		return swapList.stream().collect(Collectors.joining(","));
	}

	/**
	 * 审批流提交
	 * @param visualdevEntity
	 * @param flowTaskId
	 * @param formdata
	 * @param userInfo
	 */
	public void submitFlowTask(VisualdevEntity visualdevEntity,String flowTaskId,Object formdata,UserInfo userInfo, Map<String, List<String>> candidateList,int flowUrgent) throws WorkFlowException {
		//审批流
		if (visualdevEntity.getWebType().equals(VisualWebTypeEnum.FLOW_FROM.getType() )){
			try {
				FlowEngineService flowEngineService = SpringContext.getBean(FlowEngineService.class);
				FlowEngineEntity flowEngineEntity = flowEngineService.getInfo(visualdevEntity.getFlowId());
				FlowTaskService flowTaskService = SpringContext.getBean(FlowTaskService.class);
				FlowTaskNewService flowTaskNewService = SpringContext.getBean(FlowTaskNewService.class);
				FlowTaskEntity flowTaskEntity = flowTaskService.getInfoSubmit(flowTaskId, FlowTaskEntity::getId);
				String id = null;
				if (flowTaskEntity != null) {
					id = flowTaskEntity.getId();
				}
				String flowTitle = userInfo.getUserName() +"的"+ visualdevEntity.getFullName();
				String billNo ="#Visual"+ DateUtil.getNow();
				FlowModel flowModel = new FlowModel();
				flowModel.setId(id);
				flowModel.setFlowId(flowEngineEntity.getId());
				flowModel.setProcessId(flowTaskId);
				flowModel.setFlowTitle(flowTitle);
				flowModel.setFlowUrgent(1);
				flowModel.setBillNo(billNo);
				Map<String, Object> data = JsonUtil.entityToMap(formdata);
				flowModel.setFormData(data);
				flowModel.setFreeApproverUserId(null);
				flowModel.setCandidateList(candidateList);
				flowModel.setUserInfo(userInfo);
				flowTaskNewService.submit(flowModel);
			} catch (WorkFlowException e) {
				throw new WorkFlowException("审批流提交失败!");
			}
		}
	}

	/**
	 * 高级查询
	 * @param conditionModel
	 * @param entity
	 * @param num
	 * @return
	 */
	public Integer getCondition(SuperQueryConditionModel conditionModel, Object entity, int num) {
		QueryWrapper<T> queryWrapper = conditionModel.getObj();
		List<ConditionJsonModel> queryConditionModels = conditionModel.getConditionList();
		String op =conditionModel.getMatchLogic();
		String tableName = conditionModel.getTableName();
		List<ConditionJsonModel> useCondition = new ArrayList<>();
		for (ConditionJsonModel queryConditionModel : queryConditionModels){
			if (queryConditionModel.getTableName().equalsIgnoreCase(tableName)){
				if (queryConditionModel.getField().contains("jnpf")){
					String child = queryConditionModel.getField();
					String s1 = child.substring(child.lastIndexOf("jnpf_" )).replace("jnpf_", "" );
					queryConditionModel.setField(s1);
				}
				if (queryConditionModel.getField().startsWith("tableField")){
					String child = queryConditionModel.getField();
					String s1 = child.substring(child.indexOf("-" )+1);
					queryConditionModel.setField(s1);
				}
				useCondition.add(queryConditionModel);
			}
		}

		if (queryConditionModels.size()<1 || useCondition.size()<1){
			return num;
		}
		if (useCondition.size()>0){
			num +=1;
		}
		//处理控件 转换为有效值
		for (ConditionJsonModel queryConditionModel : useCondition) {
				String jnpfKey = queryConditionModel.getJnpfKey();
		    String fieldValue= queryConditionModel.getFieldValue();
		    if (StringUtil.isEmpty(fieldValue)){
		    	if (jnpfKey.equals(JnpfKeyConsts.CASCADER) || jnpfKey.equals(JnpfKeyConsts.CHECKBOX) || jnpfKey.equals(JnpfKeyConsts.COMSELECT) || jnpfKey.equals(JnpfKeyConsts.ADDRESS)  ){
						queryConditionModel.setFieldValue("[]");
					} else {
						queryConditionModel.setFieldValue("");
					}
					if (queryConditionModel.getSymbol().equals("like")){
						queryConditionModel.setSymbol("==");
					} else if(queryConditionModel.getSymbol().equals("notLike")) {
						queryConditionModel.setSymbol("<>");
					}
				}
			if (jnpfKey.equals(JnpfKeyConsts.DATE)){
				String startTime = "";
				if (StringUtil.isNotEmpty(fieldValue)){
					Long o1 = Long.valueOf(fieldValue);
					startTime = DateUtil.daFormat(o1);
				}
				queryConditionModel.setFieldValue(startTime);
			} else if (jnpfKey.equals(JnpfKeyConsts.CREATETIME) || jnpfKey.equals(JnpfKeyConsts.MODIFYTIME)){
				String startTime = "";
				if (StringUtil.isNotEmpty(fieldValue)) {
					Long o1 = Long.valueOf(fieldValue);
					startTime = DateUtil.daFormatHHMMSS(o1);
				}
				queryConditionModel.setFieldValue(startTime);
			} else if (jnpfKey.equals(JnpfKeyConsts.CURRORGANIZE)){
				if (StringUtil.isNotEmpty(fieldValue)){
					List<String> orgList = JsonUtil.getJsonToList(fieldValue, String.class);
					queryConditionModel.setFieldValue(orgList.get(orgList.size() - 1));
				}
			}

		}
		//反射获取数据库实际字段
		Class<?> aClass = entity.getClass();

		queryWrapper.and(tw -> {
			for (ConditionJsonModel conditionJsonModel : useCondition){
				String conditionField = conditionJsonModel.getField();
				String jnpfKey = conditionJsonModel.getJnpfKey();
				Field declaredField = null;
				try {
					declaredField = aClass.getDeclaredField(conditionField);
				} catch (NoSuchFieldException e) {
					e.printStackTrace();
				}
				declaredField.setAccessible(true);
				String field = declaredField.getAnnotation(TableField.class).value();
				String fieldValue = conditionJsonModel.getFieldValue();
				String symbol = conditionJsonModel.getSymbol();
				if ("AND".equalsIgnoreCase(op)) {
					if (symbol.equals("==")){
						tw.and(qw->{
							if (!jnpfKey.equals(JnpfKeyConsts.NUM_INPUT) && !jnpfKey.equals(JnpfKeyConsts.CALCULATE)){
									qw.eq(field, fieldValue);
							} else {
								if (StringUtil.isNotEmpty(fieldValue)){
									qw.eq(field, fieldValue);
								}
							}
							if (StringUtil.isEmpty(fieldValue)){
								qw.or(
									ew->ew.isNull(field)
								);
							}
						}
					);
					}else if (symbol.equals(">=")){
						tw.ge(field,fieldValue);
					} else if (symbol.equals("<=")){
						tw.and(ew->{
							ew.le(field,fieldValue);
							ew.and(
									qw->qw.ne(field,"")
							);
						});
					}	else if (symbol.equals(">")){
						tw.gt(field,fieldValue);
					} else if (symbol.equals("<")){
						tw.and(ew->{
							ew.lt(field,fieldValue);
							ew.and(
									qw->qw.ne(field,"")
							);
						});
					} else if (symbol.equals("<>")){
						tw.and(ew->{
							ew.ne(field,fieldValue);
							if (StringUtil.isNotEmpty(fieldValue)){
								ew.or(
										qw->qw.isNull(field)
								);
							} else {
								ew.and(
										qw->qw.isNotNull(field)
								);
							}
						});
					} else if (symbol.equals("like")){
						tw.and(ew->{
							if (StringUtil.isNotEmpty(fieldValue)){
								ew.like(field,fieldValue);
							} else {
								ew.isNull(field);
							}
						});
					} else if (symbol.equals("notLike")){
						tw.and(ew->{
							if (StringUtil.isNotEmpty(fieldValue)){
								ew.notLike(field,fieldValue);
								ew.or(
										qw->qw.isNull(field)
								);
							} else {
								ew.isNotNull(field);
							}
						});
					}
				}else {
					if (symbol.equals("==")){
						tw.or(
								qw->qw.eq(field,fieldValue)
						);
					}else if (symbol.equals(">=")){
						tw.or(
								qw->qw.ge(field,fieldValue)
						);
					} else if (symbol.equals("<=")){
						tw.or(
								qw->qw.le(field,fieldValue)
						);
					}	else if (symbol.equals(">")){
						tw.or(
								qw->qw.gt(field,fieldValue)
						);
					} else if (symbol.equals("<")){
						tw.or(
								qw->qw.lt(field,fieldValue)
						);
					} else if (symbol.equals("<>")){
						tw.or(
								qw->qw.ne(field,fieldValue)
						);
						if (StringUtil.isNotEmpty(fieldValue)){
							tw.or(
									qw->qw.isNull(field)
							);
						}
					} else if (symbol.equals("like")){
						if (StringUtil.isNotEmpty(fieldValue)){
							tw.or(
									qw->qw.like(field,fieldValue)
							);
						} else {
							tw.or(
									qw->qw.isNull(field)
							);
						}
					} else if (symbol.equals("notLike")){
						if (StringUtil.isNotEmpty(fieldValue)){
							tw.or(
									qw->qw.notLike(field,fieldValue)
							);
							tw.or(
									qw->qw.isNull(field)
							);
						} else {
							tw.or(
									qw->qw.isNotNull(field)
							);
						}
					}
				}
			}
		});
  return num;
	}

	public List<String> getIntersection(List<List<String>> lists){
		if (lists ==null || lists.size() == 0){
			return new ArrayList<>();
		}
		ArrayList<List<String>> arrayList = new ArrayList<>(lists);
		for (int i = 0; i<arrayList.size();i++){
			List<String> list =arrayList.get(i);
			if (list==null || list.size()==0){
				return new ArrayList<>();
			}
		}
		List<String> intersection = arrayList.get(0);
		for (int i = 0; i<arrayList.size();i++){
			List<String> list =arrayList.get(i);
			intersection.retainAll(arrayList.get(i));
		}
		return intersection;
	}
}
